﻿using AutoMapper;
using Microsoft.IdentityModel.Tokens;
using PowerHub.MES.Common.NumberingCenter;
using PowerHub.MES.DTO.BasicData.DTO;
using PowerHub.MES.DTO.BasicData.QueryDto;
using PowerHub.MES.EFBase;
using PowerHub.MES.Entities;
using PowerHub.MES.Entities.BasicData;
using PowerHub.MES.IRepository.IBasic;
using PowerHub.MES.IServices.IBasic;
using PowerHub.MES.Jwt;
using PowerHub.MES.ReturnEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PowerHub.MES.Services.Basic
{
    /// <summary>
    /// 角色接口实现层
    /// </summary>
    public class BasicData_RoleinfoServices : BaseServices<BasicData_Roleinfo>, IBasicData_RoleinfoServices
    {
        private IBasicData_RoleinfoRepository _ibasicData_RoleinfoRepository;
        private IBasicData_Roleinfo_MenuInfoRepository _basicData_Roleinfo_MenuInfoRepository;
        private IBasicDataUserinfoRepository _basicDataUserinfoRepository;
        private IBasicData_MenuInfoServices _basicData_MenuInfoServices;
        public UserServices _userServices;
        private IMapper _mapper;
        private static Random random = new Random();

        public BasicData_RoleinfoServices(PowerHubMesContext powerHubMesContext, IBasicData_RoleinfoRepository ibasicData_RoleinfoRepository,
            UserServices userServices, IMapper mapper, IBasicData_Roleinfo_MenuInfoRepository basicData_Roleinfo_MenuInfoRepository,
            IBasicDataUserinfoRepository basicDataUserinfoRepository, IBasicData_MenuInfoServices basicData_MenuInfoServices) : base(powerHubMesContext)
        {
            _ibasicData_RoleinfoRepository = ibasicData_RoleinfoRepository;
            _userServices = userServices;
            _mapper = mapper;
            _basicData_Roleinfo_MenuInfoRepository = basicData_Roleinfo_MenuInfoRepository;
            _basicDataUserinfoRepository = basicDataUserinfoRepository;
            _basicData_MenuInfoServices = basicData_MenuInfoServices;
        }

        /// <summary>
        /// 获取角色列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationPageResult<List<BasicData_Roleinfo>>> GetBasicData_RoleinfoPageListAsync(QueryBasicData_RoleinfoDTO query)
        {
            return await _ibasicData_RoleinfoRepository.GetBasicData_RoleinfoPageListAsync(query);
        }

        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<object>> CreateBasicData_RoleinfoAsync(BasicData_RoleinfoDTO query)
        {
            //实体转换
            var basicData_Roleinfo = _mapper.Map<BasicData_Roleinfo>(query);
            basicData_Roleinfo.Id = Guid.NewGuid().ToString();
            basicData_Roleinfo.RoleNo = GenerateBasicDataNO.GenerateCode(query.RoleName);
            basicData_Roleinfo.Creator = _userServices.UserName;

            return _ibasicData_RoleinfoRepository.CreateBasicData_RoleinfoAsync(basicData_Roleinfo);
        }



        /// <summary>
        /// 批量删除角色
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<object>> BatchDeletionBasicData_RoleinfoAsync(List<string>? ids)
        {
            if (ids.Count < 1)
            {
                throw new Exception("请先选择数据！");
            }
            return await _ibasicData_RoleinfoRepository.BatchDeletionBasicData_RoleinfoAsync(ids);
        }

        /// <summary>
        /// 更新角色
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<object>> UpdateBasicData_RoleinfoAsync(BasicData_RoleinfoDTO query)
        {
            if (query == null)
            {
                throw new Exception("请先选择数据！");
            }

            //根据id获取部门信息
            BasicData_Roleinfo basicData_Roleinfo = await GetEntityAsync(d => d.Id.Equals(query.Id));
            if (basicData_Roleinfo == null)
            {
                throw new Exception("更新部门信息发生了异常,请刷新页面后试试");
            }
            basicData_Roleinfo.RoleName = query.RoleName;
            basicData_Roleinfo.Remark = query.Remark;
            basicData_Roleinfo.Updator = _userServices.UserName;

            return _ibasicData_RoleinfoRepository.UpdateBasicData_RoleinfoAsync(basicData_Roleinfo);

        }


        /// <summary>
        /// 根据角色ID获取菜单列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<object>> GetBasicData_Roleinfo_MenuInfoByIdTreeListAsync(QueryBasicData_Roleinfo_MenuInfoDTO query)
        {
            var result = new OperationResult<object>();

            if (!string.IsNullOrEmpty(query.RoleinfoId))
            {
                //根据角色获取菜单id
                var roleinfoMenuInfoIds = await _basicData_MenuInfoServices.GetBasicData_RoleinfoById_MenuInfoListAsync(query);

                //获取菜单信息
                var menuInfos = await _basicData_MenuInfoServices.GetBasicData_MenuInfoListAsync(new QueryBasicData_MenuInfoDTO());
                List<string> ids=new List<string>() { "7419e262-f7f8-4a0b-b24f-ea3651363052", "68923d9b-b2ed-4a0c-ae8a-36ec2a5e3eef" };
                var menuInfoss= menuInfos.Data.Where(m => !ids.Contains(m.Id)).ToList();

                result.Data =new { roleinfoMenuInfoIds= roleinfoMenuInfoIds.Data, menuInfos=menuInfos.Data };
            }

            return result;
        }

        /// <summary>
        /// 根据用户ID获取角色菜单列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<List<BasicData_MenuInfoDTOs>>> GetBasicData_Roleinfo_MenuInfoListAsync()
        {
            var result = new OperationResult<List<BasicData_MenuInfoDTOs>>();

            if (string.IsNullOrEmpty(_userServices.UserId))
            {
                throw new Exception("请重新登录!");
            }

            //获取用户信息
            var basicData_Userinfo = await _basicDataUserinfoRepository.GetBasicDataUserinfoSingleAsync(_userServices.UserId);
            if (basicData_Userinfo != null && !string.IsNullOrEmpty(basicData_Userinfo.RoleinfoId))
            {
                //根据角色获取菜单id
               List<string> menuInfoIds=  await _basicData_Roleinfo_MenuInfoRepository.GetBasicData_Roleinfo_MenuInfoPageListAsync(new QueryBasicData_Roleinfo_MenuInfoDTO(basicData_Userinfo.RoleinfoId));

                //获取菜单信息
                result.Data = await _basicData_MenuInfoServices.GetBasicData_MenuInfoByIdListAsync(menuInfoIds);
            }
            
            return result;
        }
    }
}
